package com.milhouz.quizzdroid;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.HorizontalScrollView;

import com.milhouz.quizzdroid.bean.Game;

public class GameOptionActivity extends Activity {

	public static final String PREFERENCES = "GAME_OPTIONS";
	private static Game mGame;

	public static Button prefNbTeams;
	public static Button prefTextToSpeech;
	public static Button prefCountDownTime;

	public static HorizontalScrollView avatarsView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Set the fullscreen mode
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.game_options);

		prefNbTeams = (Button) findViewById(R.id.nbTeams);
		prefTextToSpeech = (Button) findViewById(R.id.activateTts);
		prefCountDownTime = (Button) findViewById(R.id.timeCountdown);

		mGame = getIntent().getExtras().getParcelable(getString(R.string.intent_extra_game));
		switch (mGame.getGameMode()) {
		case SINGLE:
			prefNbTeams.setVisibility(View.GONE);
			break;
		case MULTIPLAYER:
			prefNbTeams.setText(String.format(getString(R.string.game_settings_multiplayer_nb_teams), String.valueOf(mGame.getNbPlayers())));
			break;
		}

		Button validatePrefOptions = (Button) findViewById(R.id.validateGameOption);
		validatePrefOptions.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				Intent myIntent = new Intent(view.getContext(), QuizzDroidGameActivity.class);
				myIntent.putExtra(getString(R.string.intent_extra_game), mGame);
				startActivityForResult(myIntent, 0);
			}
		});

		if (prefNbTeams != null) {
			prefNbTeams.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					int nbPlayeurs = mGame.getNbPlayers();
					nbPlayeurs++;
					if (nbPlayeurs < 2 || nbPlayeurs > 4) {
						nbPlayeurs = 2;
					}
					mGame.setNbPlayers(nbPlayeurs);
					updateViews();
				}
			});
		}

		prefTextToSpeech.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				mGame.setTextToSpeech(!mGame.isTextToSpeech());
				updateViews();
			}
		});

		prefCountDownTime.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				// Set timer
				long time = mGame.getCountDownTime();
				time += 10000;
				if (time < 0 || time > 60000) {
					time = 0;
					mGame.setCountDown(false);
				} else {
					mGame.setCountDown(true);
				}
				mGame.setCountDownTime(time);
				updateViews();
			}
		});
		updateViews();
	}

	private void updateViews() {
		// nbTeam button text
		if (prefNbTeams != null) {
			prefNbTeams.setText(String.format(getString(R.string.game_settings_multiplayer_nb_teams), String.valueOf(mGame.getNbPlayers())));
		}
		// nbTeam button text
		if (mGame.isTextToSpeech()) {
			prefTextToSpeech.setText(getString(R.string.game_settings_audio_tts_activated));
		} else {
			prefTextToSpeech.setText(getString(R.string.game_settings_audio_tts_unactivated));
		}
		// prefCountDownTime button text
		if (mGame.getCountDownTime() == 0) {
			prefCountDownTime.setText(getString(R.string.game_settings_countdown_unactivated));
		} else {
			prefCountDownTime.setText(String.format(getString(R.string.game_settings_countdown_time), String.valueOf(mGame.getCountDownTime() / 1000)));
		}
	}
}
